﻿using System;
using Common.Utils.Helper;
using USClientKernel.Template;


namespace Game.Logic
{
    public struct GatewayInfo
    {
        public string addr;
        public string port;
        public string area;
        public string desc;
    }
}

namespace Game.Logic.Manager
{
    public class GameGatewayManager : Singleton<GameGatewayManager>
    {
        private CSharpLuaTable m_configs;
        public CSharpLuaTable Configs { get => m_configs; private set => m_configs = value; }

        private string m_curGateway;
        public string CurGateway { get => m_curGateway; private set => m_curGateway = value; }

        private int m_addrIndex;
        public int AddrIndex { get => m_addrIndex; private set => m_addrIndex = value; }


        protected override void Init()
        {
            Configs = GameConfigManager.Instance.LoadConfig(GameDefine.GAME_CONFIG_GATEWAY);
            InitGateway();
        }


        private void InitGateway()
        {
            CurGateway = GameDefine.GATEWAY_DEFAULT;
#if !DEBUG
            // todo: 获取实际区域
#endif
            AddrIndex = 1;
        }

        public GatewayInfo? GetCurGatewayInfo()
        {
            CSharpLuaTable config = Configs[CurGateway];
            if (null == config)
                return null;

            if (AddrIndex > config["addrs"].Table.Count)
                return null;

            GatewayInfo info;
            CSharpLuaTable addrInfo = config["addrs"][AddrIndex.ToString()];
            info.addr = addrInfo.GetField("addr");
            info.port = addrInfo.GetField("port");
            info.area = config.GetField("area");
            info.desc = config.GetField("desc");

            return info;
        }
    }
}
